package
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	import game.core.ConfigManager;
	import game.core.NetWorkManager;
	import game.core.PrintLog;
	
	import org.bulkLoader.comply.BulkFile;
	import org.bulkLoader.comply.BulkLoader;
	import org.bulkLoader.events.BulkEvent;
	import org.bulkLoader.events.BulkItemLoadingEvent;
	
	[SWF(frameRate="24",height="600",width="800",backgroundColor="0")]
	public class GamePrepare extends Sprite
	{
		protected var loader:BulkLoader;
		protected var container:Sprite;
		protected var loginView:LoginView;
		
		public function GamePrepare()
		{
			container = new Sprite();
			addChild(container);
			
			loader = new BulkLoader("gameloader");
			loadConfigXML("assets/config.xml");
		}
		
		private function loadConfigXML(param0:String):void
		{
			loader.addEventListener(BulkEvent.ITEM_COMPLETED, loadConfigXMLComplete);
			loader.add(param0, {});
			loader.execute();
		}
		
		protected function loadConfigXMLComplete(event:BulkItemLoadingEvent):void
		{
			PrintLog.show("configuration file has loaded succesfully.");
			loader.removeEventListener(BulkEvent.ITEM_COMPLETED, loadConfigXMLComplete);
			
			var xmlData:XML = event.loadingInfo.getDataAsXml();
			ConfigManager.setData(xmlData);
			
			connectServer();
		}
		
		private function connectServer():void
		{
			var host:String = ConfigManager.serverHost;
			var port:int = ConfigManager.serverPort;
			PrintLog.show("connect server["+host+":"+port+"].");
			
			var net:NetWorkManager = NetWorkManager.getInstance();
			net.setConnectSuccesfullyHandler(connectSuccesfullyHandler);
			net.setConnectFaultHandler(connectFaultHandler);
			net.connect(host, port);
		}
		
		private function connectFaultHandler():void
		{
			PrintLog.show("failed to connect to server.");
		}
		
		private function connectSuccesfullyHandler():void
		{
			PrintLog.show("succesfully to connect to server.");
			prepareLoginUI();
		}
		
		private function prepareLoginUI():void
		{
			loginView = new LoginView();
			container.addChild(loginView);
		}
	}
}